package com.unicom.room.controller;

import com.unicom.room.common.JsonResult;
import com.unicom.room.model.HistoryQueryVo;
import com.unicom.room.service.HistoryRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;


@RestController
@RequestMapping("/hotelMgmt/historyRecordMgmt")
public class HistoryRecordController {
	
	@Autowired
	private HistoryRecordService historyRecordService;
	
	private static final Logger logger = LoggerFactory.getLogger(HistoryRecordController.class);
	
	/**
	 * 查询当前房间开房情况
	 * @return
	 */
	@RequestMapping(value = "/listHistoryRecords", method = RequestMethod.POST)
	public JsonResult listHistoryRecords(@RequestBody HistoryQueryVo historyQueryVo) {
		SimpleDateFormat sdf;
		StringBuilder dateFormat, mysqlFormat=null;
		Date dt = null;
		if(historyQueryVo.getStartDate() != null) {
			final String[] formatStrings = {"yyyy", "MM", "dd"};
			final String[] mysqlDateString = {"%Y", "%m", "%d"};
			dateFormat = new StringBuilder(formatStrings[0]);
			mysqlFormat = new StringBuilder(mysqlDateString[0]);
			for (int i = 1; i < historyQueryVo.getStartDate().split("-").length; i++) {
				dateFormat.append("-").append(formatStrings[i]);
				mysqlFormat.append("-").append(mysqlDateString[i]);
			}
			sdf = new SimpleDateFormat(dateFormat.toString());
			sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
			try {
				dt = sdf.parse(historyQueryVo.getStartDate());
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}

		try {
			return new JsonResult("200", historyRecordService.listHistoryRecords(historyQueryVo.getCurrentPage(),historyQueryVo.getPageSize()
					, historyQueryVo.getName(), dt, mysqlFormat==null?null:mysqlFormat.toString()));
		} catch (Exception e) {
			logger.error("查询历史记录异常", e);
			return new JsonResult("500");
		}
	}

}
